package com.cl.edu.web;

import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;

import com.cl.core.orm.Page;
import com.cl.edu.entity.News;
import com.cl.edu.entity.Newssort;
import com.cl.edu.service.IndexManager;
import com.cl.edu.service.admin.NewsManager;
import com.opensymphony.xwork2.ActionSupport;

public class ContentAction extends ActionSupport{
	@Autowired
	private NewsManager newsManager;
	@Autowired
	private IndexManager indexManager;
	
	private Page<News> page = new Page<News>();	//内容列表
	private String filter_content;
	private String filter_typeid;
	private Long flag = 1L; //标识是栏目列表还是搜索列表 1表示栏目 2表示搜索 默认为栏目
	
	private News			content;			//当前内容
	private News			preContent;			//上一条内容 
	private News			nextContent;		//下一条内容
	private Long			id;					//当前内容id/栏目id
	
	private List<Newssort> newssortList;
	
	private List<News> list	  ;					//栏目列表
	private List<News> list1  ;					//右边栏列表1
	private List<News> list2  ;					//右边栏列表2
	private List<News> list3  ;					//右边栏列表3
	private List<News> list4  ;					//右边栏列表4
	
	public String execute() throws Exception {
		if (!page.isOrderBySetted())		{
			page.setOrderBy("id");
		}
		content = newsManager.getNewsById(id);
		content.setReadNum(content.getReadNum()+1);
		newsManager.saveOrUpdateNews(content); //更新阅读次数
		
		Long parentId = content.getChannel().getId();
		preContent = newsManager.preContent(page, id, parentId);
		nextContent = newsManager.nextContent(page, id, parentId);
		
		initRight();
		
		return SUCCESS;
	}
	
	/**
	 * 栏目信息列表
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		if (!page.isOrderBySetted())		{
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		int pageNo = page.getPageNo();
		initRight();	//在初始化其它列表时会覆盖当前页数据，使分页不能正常进行
		page.setPageNo(pageNo);
		
		page = indexManager.getNewsById(page, id, 20);
		list = page.getResult();
		
		return "list";
	}
	
	public String search() throws Exception {
		if (!page.isOrderBySetted())		{
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		int pageNo = page.getPageNo();
		initRight();	//在初始化其它列表时会覆盖当前页数据，使分页不能正常进行
		page.setPageNo(pageNo);
		
		page.setPageSize(20);
		page = newsManager.getNewsList(page);
		list = page.getResult();
		return "list";
	}
	
	//公用右边栏信息
	private void initRight() {
		if (!page.isOrderBySetted())		{
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = indexManager.getNewsById(page, 1L, 6);
		list1 = page.getResult();
		
		page = indexManager.getNewsById(page, 13L, 6);
		list2 = page.getResult();
		
		
		newssortList = newsManager.getNewssorts();
//		int channelId = new Random().nextInt(13)+2;
//		
//		page = indexManager.getNewsById(page, 1l, 6);
//		list1 = page.getResult();
//		
//		page = indexManager.getNewsById(page, (long)(channelId), 6);
//		list2 = page.getResult();
//		
//		page = indexManager.getNewsById(page, (long)(channelId+1), 6);
//		list3 = page.getResult();
//		
//		page = indexManager.getNewsById(page, (long)(channelId+2), 6);
//		list4 = page.getResult();
//		
//		newssortList = newsManager.getNewssorts();
	}
	public Page<News> getPage() {
		return page;
	}
	public void setPage(Page<News> page) {
		this.page = page;
	}
	public News getContent() {
		return content;
	}
	public void setContent(News content) {
		this.content = content;
	}
	public News getPreContent() {
		return preContent;
	}
	public void setPreContent(News preContent) {
		this.preContent = preContent;
	}
	public News getNextContent() {
		return nextContent;
	}
	public void setNextContent(News nextContent) {
		this.nextContent = nextContent;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public List<News> getList1() {
		return list1;
	}
	public void setList1(List<News> list1) {
		this.list1 = list1;
	}
	public List<News> getList2() {
		return list2;
	}
	public void setList2(List<News> list2) {
		this.list2 = list2;
	}
	public List<News> getList3() {
		return list3;
	}
	public void setList3(List<News> list3) {
		this.list3 = list3;
	}
	public List<News> getList4() {
		return list4;
	}
	public void setList4(List<News> list4) {
		this.list4 = list4;
	}
	public List<Newssort> getNewssortList() {
		return newssortList;
	}
	public void setNewssortList(List<Newssort> newssortList) {
		this.newssortList = newssortList;
	}

	public List<News> getList() {
		return list;
	}

	public void setList(List<News> list) {
		this.list = list;
	}

	public String getFilter_content() {
		return filter_content;
	}

	public void setFilter_content(String filter_content) {
		this.filter_content = filter_content;
	}

	public String getFilter_typeid() {
		return filter_typeid;
	}

	public void setFilter_typeid(String filter_typeid) {
		this.filter_typeid = filter_typeid;
	}

	public Long getFlag() {
		return flag;
	}

	public void setFlag(Long flag) {
		this.flag = flag;
	}
	
	
}
